<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

class ActivityPrize extends SortableModel
{
    use HasFactory;

    public static $levelMap = [
        1  => '一等奖',
        2 => '二等奖',
        3 => '三等奖',
        4  => '四等奖',
        5 => '五等奖',
        6 => '六等奖',
        7 => '七等奖',
        8 => '八等奖',
        9 => '九等奖',
    ];

    const TYPE_NORMAL = 'normal';
    const TYPE_COUPON = 'coupon';
    const TYPE_VIRTUAL = 'virtual';

    public static $typeMap = [
        self::TYPE_NORMAL  => '实体奖品',
        self::TYPE_COUPON => '卡券奖品',
        self::TYPE_VIRTUAL => '虚拟奖品',
    ];

    const LIMIT_DAILY = 'daily';
    const LIMIT_WEEKLY = 'weekly';
    const LIMIT_MONTHLY = 'monthly';
    const LIMIT_YEARLY = 'yearly';
    const LIMIT_TOTAL = 'total';

    public static $limitMap = [
        self::LIMIT_DAILY  => '每日',
        self::LIMIT_WEEKLY => '每周',
        self::LIMIT_MONTHLY => '每月',
        self::LIMIT_YEARLY => '每年',
        self::LIMIT_TOTAL => '固定数'
    ];

    public function getLevelChAttribute()
    {
        return self::$levelMap[$this->level];
    }

    protected $fillable = [
        'activity_id', 'level', 'name', 'description', 'image', 'little_image', 'probability', 'stock', 'order', 'valid', 'limit', 'limit_value', 'type'
    ];

    protected $casts = [
        'activity_id' => 'integer',
        'level' => 'integer',
        'order' => 'integer',
    ];

    public function decreaseStock()
    {
        return $this->where('id', $this->id)->where('stock', '>=', 1)->decrement('stock', 1);
    }

    public function activity()
    {
        return $this->belongsTo(Activity::class);
    }
}
